<?php

// +---------------------------------------------+
// |     Copyright  2010 - 2018 InterPhoto       |
// |     http://www.weentech.com                 |
// |     This file may not be redistributed.     |
// +---------------------------------------------+


include('includes/InterPhoto.Core.php');


///////////////////////////////////////////////////////////////////////////// FUNCTIONS /////////////////////////

function GetImageCounts($categoryid){
	global $siteicategories;

	if($categoryid){
		$sReturn = $siteicategories[$categoryid]['images'];
	}else{
		foreach($siteicategories as $value){
			$sReturn += $value['images'];
		}
	}

	return $sReturn;
}

function GetCategorySelect($selectname, $selectedid = 0){
	global $sys_langs;
	$sReturn = '<select name="' . $selectname . '" onchange="if(this.options[this.selectedIndex].value >= 0) {document.location=\''. GetUrl('index.php') . '?c=\'+this.options[this.selectedIndex].value;}">';
	$sReturn .= '<option value="0">'.$sys_langs['allcategory']  . ' (' . GetImageCounts(0) . ')</option>';
	$sReturn .= GetOptions($selectedid);
	$sReturn .= '</select>';

	return $sReturn;
}

function GetOptions($selectedid = 0, $parentid = 0, $sublevelmarker = ''){
	global $siteicategories;

	if($parentid) $sublevelmarker .= '&minus;&minus;';

	$allsiteicategories = $siteicategories;
	foreach($allsiteicategories as $key => $value){
		if($parentid == $value['parentid']){
			$thiscounts = GetImageCounts($key);
			$thiskey = Iif($thiscounts, $key, -1);
			$sReturn .= '<option '. Iif(!$parentid, 'style="color:#FF3300;font-weight:bold;"') . ' value="' . $thiskey . '" ' . Iif($selectedid == $key, 'SELECTED', '') . '>' . $sublevelmarker . ShortTitle($value['title'], 12) . ' (' . $thiscounts . ')</option>';

			$sReturn .= GetOptions($selectedid, $key, $sublevelmarker);
		}
	}

	return $sReturn;
}

function GetCategorylinks($categoryid){
	global $siteicategories, $langs;

	$sReturn = ' ' . $langs['nav'] . ' <a href="'.GetUrl('index.php', 'c', $categoryid).'">'.ShortTitle($siteicategories[$categoryid]['title'], 18).'</a>';

	if($siteicategories[$categoryid]['parentid']){
		$sReturn = GetCategorylinks($siteicategories[$categoryid]['parentid']) . $sReturn;
	}

	return $sReturn;
}

///////////////////////////////////////////////////////////////////////////// FUNCTIONS /////////////////////////

$smarty = new InterPhoto;

$siteicategories = array();

$geticategories = $DB->query("SELECT c.categoryid, c.parentid, c.title, c.keywords, c.sorting, COUNT(i.imageid) AS images FROM " . TABLE_PREFIX . "categories c LEFT JOIN " . TABLE_PREFIX . "images i ON (i.categoryid = c.categoryid AND i.actived = 1) WHERE c.actived = 1 GROUP BY c.categoryid ORDER BY c.ordernum ASC");

while($icategory = $DB->getrow($geticategories)){
	$siteicategories[$icategory['categoryid']] = $icategory;
}

$categoryid = ForceIncomingInt('c');
$categoryselect = GetCategorySelect('categoryselect', $categoryid);


$page = ForceIncomingInt('p', 1);
$search = ForceIncomingString('s');

$NumPerPage = ForceInt(ForceIncomingCookie('IPP'.COOKIE_KEY), ForceInt($mainsettings['siteNumPerpage'], 10));
$orderby = ForceIncomingCookie('IPS'.COOKIE_KEY);

if($categoryid AND $siteicategories[$categoryid]['categoryid']){
	$categorysql .= " AND i.categoryid = $categoryid ";

	$categorylinks = GetCategorylinks($categoryid);

	$smarty->assign('pagetitle', Iif($siteicategories[$categoryid]['title'], $siteicategories[$categoryid]['title'] . ' - ') .$mainsettings['siteTitle']);
	if($siteicategories[$categoryid]['keywords']){
		$smarty->assign('metakeywords', $siteicategories[$categoryid]['keywords'] . ',' .$mainsettings['siteKeywords']);
		$smarty->assign('metadescription', $siteicategories[$categoryid]['keywords'] . ',' .$mainsettings['siteMeta']);
	}

	if(!$orderby){
		$orderby = $siteicategories[$categoryid]['sorting'];
	}
}

$orderbysql = " ORDER BY ";

switch ($orderby)
{
	case 'tdown':
		$orderbysql .= " i.imageid DESC ";
		break;
	case 'tup':
		$orderbysql .= " i.imageid ASC ";
		break;
	case 'vdown':
		$orderbysql .= " i.viewcount DESC ";
		break;
	case 'vup':
		$orderbysql .= " i.viewcount ASC ";
		break;
	case 'ddown':
		$orderbysql .= " i.downloadcount DESC ";
		break;
	case 'dup':
		$orderbysql .= " i.downloadcount ASC ";
		break;
	case 'oup':
		$orderbysql .= " i.ordernum ASC ";
		break;
	default:
		$orderbysql .= " i.ordernum DESC ";
		break;
}

if($search){
	$search = SafeSearchSql(Iif(IsGet('s'), urldecode($search), $search));

	$keywords = explode (' ', str_replace(array('+', ',' , ';'), ' ', $search) );
	foreach ($keywords as $key => $value) {
		$searchsql .= " AND (i.username LIKE '%".$value."%' OR i.title LIKE '%".$value."%' OR i.description LIKE '%".$value."%' OR i.keywords LIKE '%".$value."%') ";
	}
}

$start = $NumPerPage * ($page-1);

$getimages = $DB->query("SELECT i.imageid, i.categoryid, i.path, i.filename, i.title, i.viewcount, i.downloadcount, i.created, c.title AS categorytitle FROM " . TABLE_PREFIX . "images i LEFT JOIN  " . TABLE_PREFIX . "categories c ON (c.categoryid = i.categoryid) WHERE (i.usergroupids = 'all' OR i.usergroupids LIKE '%(".$userinfo['groupid'].")%') AND i.actived = 1 AND c.actived = 1 " .$categorysql. $searchsql . $orderbysql ." LIMIT $start,$NumPerPage");
$maxrows = $DB->query_first("SELECT COUNT(i.imageid) AS value FROM " . TABLE_PREFIX . "images i LEFT JOIN  " . TABLE_PREFIX . "categories c ON (c.categoryid = i.categoryid) WHERE (i.usergroupids = 'all' OR i.usergroupids LIKE '%(".$userinfo['groupid'].")%') AND i.actived = 1 AND c.actived = 1 ".$categorysql.$searchsql);
$images = $DB->getrows($getimages);

$totalpages = ceil($maxrows['value'] / $NumPerPage);

$pagenav = '<a href="'.GetUrl('index.php').'">'.$langs['home'].'</a>'
	.$categorylinks
	.Iif($search, ' '.$langs['nav'].' <span class=navtitle_b>'.$langs['search'].'</span>: <span class=red>'. $search. '</span>&nbsp;&nbsp;(' . $maxrows['value'] . $langs['results'] . ')');

$searchquery = Iif($search, 's='.urlencode($search));

$smarty->assign('pagenav', $pagenav);
$smarty->assign('orderby', $orderby);
$smarty->assign('searchquery', $searchquery);
$smarty->assign('numperpage', $NumPerPage);
$smarty->assign('categoryselect', $categoryselect);
$smarty->assign('categoryid', $categoryid);
$smarty->assign('userinfo', $userinfo);

if(!$maxrows['value']){
	$errors = $sys_langs['noimages'];
}else{
	$smarty->assign('pagelist', GetPageList('index.php', $totalpages, $page, 10, 'c', $categoryid, 's', urlencode($search)));
	$smarty->assign('images', $images);
}

$smarty->interPlay('index.tpl');

?>